package Cluedo.Game;
import java.util.ArrayList;
import java.util.Vector;

/**
 * The detective pad holds a copy of every card in the game so that the player
 * may at his own discretion mark a suspected item.
 *
 * @author alex
 */
public class DetectivePad
		implements Cluedo.API.Persistant {
	
	private Player lnkPlayer;

	/**
	 * CardColletcion here contains all cards in the detective pad
	 */
	CardCollection cardBag = null;
	
	String[] notes = null;
	
	public boolean notesOn = true;
	
	public DetectivePad() {
		cardBag = new CardCollection();
	}

	/**
	 * Calls the eliminateCard function on the relevant card after asking
	 * the CardCollection for the specfied card.
	 * 
	 * Card = CardCollection.getCardByName(nameofCard);
	 * Card.eliminateCard();
	 * 
	 * @param cardName
	 */
	public void markCard(String cardName) {
	  try {
	      cardBag.markCard(cardName);

	  }
	  catch(Exception error) {
		  System.out.println("[DetectivePad:markCard] Error in markcard");
	  }
	}
	/**
	 * Takes a card's name and returns the object if found.
	 * 
	 * @param cardName
	 * @return the card object, or null
	 */
	public Card hasCard(String cardName) {
		return cardBag.cardByName(cardName);
	}
	/**
	 * Calls the eliminateCard function on the relevant card after asking
	 * the CardCollection for the specfied card.
	 * 
	 * Card = CardCollection.getCardByName(nameofCard);
	 * Card.eliminateCard();
	 * 
	 * @param cardName
	 */
	public void unMarkCard(String cardName) {
		cardBag.cardByName(cardName).eliminateCard(false);
	}

	public void setCardBag(CardCollection bag) {
		cardBag = bag;
		newNotes(cardBag.countCards());
	}
	/**
	 * Adds a Vector of cards to the CardCollection, this method may be called at any occasion.
	 * @param newCards
	 */
	public void addItems(Vector newCards) {
		for(int i = 0; i < newCards.size(); i++)
			cardBag.insertCard((Card)newCards.elementAt(i));
		
		newNotes(cardBag.countCards());
	}
	
	public void addItems(CardCollection newCards) {
		for(int i = 0; i < newCards.countCards(); i++)
			cardBag.insertCard(newCards.cardAt(i));
		
		newNotes(cardBag.countCards());
	}	
	/**
	 * Returns a list of all cards and the eliminated status for displaying either in the GUI or for the GameController.
	 *  
	 */
	public CardCollection getCardList() {
		return cardBag;
	}
	
	/**
	 * Marks all cards as not eliminated.
	 * 
	 */
	public void resetCards() {
		for(int i =0;i<cardBag.countCards();i++) {
			cardBag.cardAt(i).setEliminated(false);
		}
	}
	
	private void newNotes(int size) {
		notes = new String[cardBag.countCards()];
		for(int i=0;i< notes.length; i++) {
			notes[i] = "";
		}
	}
	
	public void setNotes(ArrayList notes) {
		newNotes(notes.size());
		for(int i=0;i<notes.size();i++) {
			this.notes[i] = (String)notes.get(i);
		}
	}
	
	public void updateNotes(String[] notes) {
		for(int i=0;i<notes.length;i++) {
			this.notes[i] = notes[i];
		}
	}
	
	public void setNote(int index, String message) throws Exception {
		if(index >= notes.length)
			throw new Exception("Out of bounds: " + index + ", max = " + notes.length);
		
		notes[index] = message;
		
	}
	
	public String getNote(int index) throws Exception {
		if(index >= notes.length)
			throw new Exception("Out of bounds: " + index + ", max = " + notes.length);
		
		return notes[index];
	}
	
	public String[] getNotes() {
		return notes;
	}

	
	public String toXML() {
		String output;
		if(notesOn == false) {
			output = "   <detectivepad>\n" +
			cardBag.toXML() +
			"</detectivepad>\n";
		}
		else {
			output = "   <detectivepad>\n" +
			cardBag.toXML() + "\n"+
			outputNotes() +
			"</detectivepad>\n";
			
		}

		return output;
	}

	/**
	 * @return
	 */
	private String outputNotes() {
		StringBuffer cardMessages = new StringBuffer();
		
		cardMessages.append( "  <notes>\n" );
		
		for(int i=0;i<notes.length;i++){
			cardMessages.append( "    <note card=\""+i+"\" message=\""+notes[i]+"\"/>\n" );
		}
		
		cardMessages.append( "  </notes>\n" );
		
		return cardMessages.toString();
	}



}
